require 'spec_helper'
require 'vector'

describe Vector, "using default constructor" do
  before(:each) do
    @vec = Vector.new
  end
  
  it "should be creatable with default constructor" do
    @vec.should be_instance_of(Vector)
  end
  
  it "should be creatable with a point - initializing point, instead of l & a" do
    v = Vector.new(Point.new(0, 0))
    v.point.to_a.should == [0, 0]
    v.length.should == 0
    v.angle.should == 0
  end
  
  it "should be creatable with an array - initializing point, instead of l & a" do
    v = Vector.new([0, 0])
    v.point.to_a.should == [0, 0]
    v.length.should == 0
    v.angle.should == 0
  end
  
  it "should initialize #length" do
    @vec.should respond_to(:length)
    @vec.length.should == 0
  end
  
  it "should initialize #angle" do
    @vec.should respond_to(:angle)
    @vec.angle.should == 0
  end
  
  it "should have l aliased to length, and a aliased to angle" do
    @vec.l.should == @vec.length
    @vec.a.should == @vec.angle
  end
  
  it "should respond to #point" do
    @vec.should respond_to(:point)
    @vec.point.should be_instance_of(Point)
  end
  
  it "should invalidate length and angle on point change" do  
    @vec.should_receive(:angle=).with(nil)
    @vec.should_receive(:length=).with(nil)
    
    @vec.angle
    @vec.length
    @vec.point = Point.new(0, 0)
  end
  
  it "should not invalidate length and angle if point is set to nil" do    
    @vec.should_not_receive(:angle=)
    @vec.should_not_receive(:length=)
    
    @vec.angle
    @vec.length
    @vec.point = nil
  end
  
  it "should invalidate point on length change" do  
    @vec.should_receive(:point=).with(nil)

    @vec.point
    @vec.length = 10
  end

  it "should not invalidate point if length is set to nil" do
    @vec.should_not_receive(:point=)
    
    @vec.point
    @vec.length = nil
  end
  
  it "should invalidate point on angle change" do  
    @vec.should_receive(:point=).with(nil)

    @vec.point
    @vec.angle = 30
  end
  
  it "should not invalidate point if angle is set to nil" do
    @vec.should_not_receive(:point=)
    
    @vec.point
    @vec.angle = nil
  end
end